import 'dart:async';

import 'package:aichat_flutter_wallet/module/module_wallet/pages/no_wallet.dart';
import 'package:aichat_flutter_wallet/module/module_wallet/pages/wallet_assets_page.dart';
import 'package:aichat_flutter_wallet/module/module_wallet/provider/wallet_index_provider.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/loading/loading_weiget.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/public_tip/public_tip.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';


class WalletPage extends StatefulWidget {
  const WalletPage({super.key});

  @override
  State<WalletPage> createState() => _WalletPageState();
}

class _WalletPageState extends State<WalletPage> {
  Timer? successShowBlockTimer;

  @override
  void dispose() {
    if (successShowBlockTimer != null) successShowBlockTimer!.cancel();
    /// 取消路由订阅
    super.dispose();
  }

  // 成功 回调组件
  Widget SuccessBlockWidget() {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.w), // 设置圆弧的半径
        // 可以添加其他装饰样式，例如背景颜色、边框等
        color: Colors.white,
      ),
      margin: EdgeInsets.only(left: 33.w, bottom: 27.w, right: 33.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Column(
            children: [
              SizedBox(
                height: 10.w,
              ),
              PublicImage(
                image: 'assets/images/wallet/success_create.png',
                width: 120.w,
                height: 120.w,
              ),
              SizedBox(
                height: 10.w,
              ),
              Text(
                "Congratulations!",
                style: TextStyle(
                    fontSize: 14.sp,
                    height: 20.w / 14.w,
                    fontWeight: FontWeight.w500,
                    color: Color.fromRGBO(51, 51, 51, 1)),
              ),
              SizedBox(
                height: 10.w,
              ),
              Text(
                "You have Created the Wallet",
                style: TextStyle(
                    fontSize: 12.sp,
                    height: 16.w / 12.w,
                    fontWeight: FontWeight.w400,
                    color: Color.fromRGBO(134, 131, 145, 1)),
              ),
              SizedBox(
                height: 20.w,
              ),
            ],
          )
        ],
      ),
    );
  }

  // 创建钱包成功的 回调 页面
  void handleCreateReCall(String status) {
    if (status == 'Cancel') {
      return;
    }
    if (status == 'Failed') {
      Toast.show(context,
          status: '0',
          position: Dpositon.top,
          message: "Generation failed. Please create a new wallet",
          duration: const Duration(milliseconds: 4000));
      return;
    }

    Future.delayed(Duration(milliseconds: 600), () {
      Utils.showSheet(context, SuccessBlockWidget(),
              scrollControlled: false,
              canPop: false,
              isDismissible: true,
              bodyColor: Colors.transparent,
              bodyPadding: EdgeInsets.only(bottom: 30.w, left: 0, right: 0))
          .then((value) {
        if (successShowBlockTimer != null) {
          successShowBlockTimer!.cancel();
          successShowBlockTimer = null;
        }
        ;
      });
      successShowBlockTimer = Timer(Duration(seconds: 3), () {
        if (successShowBlockTimer == null) {
          return;
        }
        if (successShowBlockTimer != null) successShowBlockTimer!.cancel();
        Navigator.pop(context);
        // 在2秒后执行的代码
      });
      //   // 在这里添加要延迟执行的代码
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<WalletIndexProvider>(
      builder: (c, model, _) {
        if (!model.isSetWallet) {
          return FutureBuilder<WalletInfo?>(
            future: model.checkInitWalletStatus(),
            builder: (_, snapshot) {
              return Stack(
                children: [
                  NoWalletPage(
                    popCallBack: handleCreateReCall,
                  ),
                  Visibility(
                      visible:
                          snapshot.connectionState == ConnectionState.waiting,
                      child: LoadingWidget())
                ],
              );
            },
          );
        }
        return WalletAssetsPage();
      },
    );
  }
}
